package com.liukai.personalbills.adapter;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.liukai.personalbills.R;
import com.liukai.personalbills.bean.TypeBean;
import com.liukai.personalbills.util.TextUtil;

import java.util.ArrayList;

import de.hdodenhof.circleimageview.CircleImageView;

public class AddRecyclerViewAdapter extends RecyclerView.Adapter<AddRecyclerViewAdapter.MyViewHolder> {

    private ArrayList<TypeBean> typeBeanArrayList;
    private Context context;

    public AddRecyclerViewAdapter(Context context, ArrayList<TypeBean> typeBeanArrayList) {
        this.context = context;
        this.typeBeanArrayList = typeBeanArrayList;
    }


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = View.inflate(viewGroup.getContext(),R.layout.item_recyclerview_add,null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, final int i) {

        TypeBean typeBean = typeBeanArrayList.get(i);
        myViewHolder.imgIcon.setImageResource(typeBean.getImgIcon());
        myViewHolder.tvType.setText(typeBean.getType());
        myViewHolder.btnType.setOnClickListener(view -> {
            // 修改图标颜色
//            for (TypeBean bean : typeBeanArrayList) {
//                // 将所有图标的样式设置为默认样式
//                myViewHolder.imgIcon.setImageResource(TextUtil.getNormalIcon(typeBean.getType()));
//                notifyDataSetChanged();
//            }
            // 将点击后的图标设置为点击样式
//            myViewHolder.imgIcon.setImageResource(TextUtil.getClickIcon(typeBean.getType()));

            onItemClick.onItemClickListener(typeBeanArrayList.get(i),i);
        });

    }

    @Override
    public int getItemCount() {
        return typeBeanArrayList.size();
    }

    /**
     * 获取该位置的图标样式并修改
     * @param bean 传递的消费类别
     * @param position 需要修改图标样式的位置
     */
    public void setTypeIcon(TypeBean bean, int position) {
        typeBeanArrayList.get(position).setImgIcon(bean.getImgIcon());
    }

    /**
     * 修改所有的消费类别的图标样式
     * @param typeBeanArrayList
     */
    public void setAllTypeIcon(ArrayList<TypeBean> typeBeanArrayList) {
        this.typeBeanArrayList = typeBeanArrayList;
        notifyDataSetChanged();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        public LinearLayout btnType;
        public TextView tvType;
        public CircleImageView imgIcon;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            btnType = itemView.findViewById(R.id.btnType);
            imgIcon = itemView.findViewById(R.id.imgIcon);
            tvType = itemView.findViewById(R.id.tvType);

        }


    }

    public interface OnItemClick{
        /**
         *
         * @param typeBean 消费类别
         * @param position 点击的消费类别的位置
         */
        public void onItemClickListener(TypeBean typeBean,int position);
    }

    private OnItemClick onItemClick;

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }
}
